$(function() {
	"use strict";
	
	var userDataTable;
	
	var initUserTable = function () {

        var table = $('#user_table');

        userDataTable = table.DataTable({
        	"searching": false,
            "language": {
                "emptyTable": "暂无数据",
                "loadingRecords": "请稍候 - 数据加载中...",
                "info": "显示 _START_ 到 _END_ 共 _TOTAL_ 条记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "lengthMenu": "_MENU_",
                "zeroRecords": "暂无数据",
                "paginate": {
                    "previous":"上一页",
                    "next": "下一页",
                    "last": "最后一页",
                    "first": "第一页"
                }
            },
//            "dom": '<<t>ilp>',
//            "lengthMenu": [
//                [6, 15, 20, -1],
//                [6, 15, 20, "All"] // change per page values here
//            ],
            "ordering": false,
            "lengthChange":false,
            "pageLength": 20,
            "pagingType": "full_numbers",
            "serverSide": true,
            "ajax": {
                url: context+"/console/user/list",
                type: 'POST',
                data:function(d){
                	var queryForm = $(table.data("queryForm"));
                	var x=queryForm.serializeArray();
            	    $.each(x, function(i, field){
            		 d[field.name] = field.value;
            	    });
                },
                dataSrc: 'userList'
            },
            "columns": [
                { "data": "userNumber" },
                { "data": "userName" },
                { "data": "loginName" },
                { "data": "sex" ,
                  "render": function ( data, type, full, meta ) {
                	  if(data=='M')
                		  return '男';
                	  else if(data=='F')
                		  return '女';
                	  
                	  return '';
                  }
                },
                { "data": "orgNamePath" },
                { "data": "userId",
                  "width":280,
                  "render": function ( data, type, full, meta ) {
                    return '<a href="javascript:;" class="btn btn-default btn-sm edit" data-edit-id='+data+'><i class="fa fa-edit"></i> 编辑用户 </a>&nbsp;'+
                    	   '<a href="javascript:;" class="btn btn-danger btn-sm remove" data-remove-id="'+data+'"><i class="fa fa-remove"></i> 删除用户 </a>&nbsp;'+
                    	   '<a href="javascript:;" class="btn btn-default btn-sm reset" data-user-id="'+data+'"><i class="fa fa-eraser"></i> 密码重置 </a>&nbsp;'
                  }
                }
            ]
        });
    }
	
	var initAddUser = function(){
		$(".addUser").on('click',function(){
			var dialog = $("#userAddDialog");
			var form = $("#userForm",dialog);
			form[0].reset();
			dialog.modal();
		});
		
		$(".ok","#userAddDialog").on('click',function(){
			var dialog = $("#userAddDialog");
			var form = $("#userForm",dialog);
			
			App.blockUI({
                target: '#userAddDialogContent',
                boxed: true,
                message: '数据处理中...',
                textOnly: true
            });
			
			$.ajax({
	            url:context+'/console/user/save',
	            type:'POST',
	            data:form.serialize(),
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#userAddDialogContent');
	                if(data.orgId){
	                	toastr["info"]("保存用户成功", "提示");
	                	userDataTable.ajax.reload();
	                	dialog.modal('hide');
	                }else{
	                	toastr["error"]("保存用户异常", "提示");
	                }
	                
	            }
	        });
		});
	}
	
	var initEditUser = function(){
		$("#user_table").delegate('.edit','click',function(){
			
			var editId = $(this).data("editId");
			
			$("#userEditForm")[0].reset();
			var dialog = $("#userEditDialog");
			dialog.modal();
			App.blockUI({
                target: '#userEditDialogContent',
                boxed: true,
                message: '数据加载中...',
                textOnly: true
            });
			$.ajax({
	            url:context+'/console/user/load/'+editId,
	            type:'POST',
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#userEditDialogContent');
	                if(data.success){
	                	iTsai.form.deserialize($('#userEditForm'),data.data)
	                	$(".jstreeselect-input",$("#userEditForm")).each(function(){
	                		var val = $(this).val();
	                		$(this).data('jstree').deselect_all(); 
	                		$(this).jstreeVal(val);
	                	});
	                }else{
	                	toastr["error"]("加载用户数据异常", "提示");
	                }
	                
	            }
	        });
			
		});
		
		
		$(".ok","#userEditDialog").on('click',function(){
			var dialog = $("#userEditDialog");
			var form = $("#userEditForm",dialog);
			
			App.blockUI({
                target: '#userEditDialogContent',
                boxed: true,
                message: '数据处理中...',
                textOnly: true
            });
			
			$.ajax({
	            url:context+'/console/user/save',
	            type:'POST',
	            data:form.serialize(),
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	            	App.unblockUI('#userEditDialogContent');
	                if(data.orgId){
	                	toastr["info"]("保存用户成功", "提示");
	                	userDataTable.ajax.reload();
	                	dialog.modal('hide');
	                }else{
	                	toastr["error"]("保存用户异常", "提示");
	                }
	                
	            }
	        });
		});
	}
	
	var initRemoveUser = function(){
		$("#user_table").delegate('.remove','click',function(){
			
			var removeId = $(this).data("removeId");
			
			$.ajax({
	            url:context+'/console/user/delete/'+removeId,
	            type:'POST',
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	                if(data.success){
	                	toastr["info"]("删除用户成功", "提示");
	                	userDataTable.ajax.reload();
	                }else{
	                	toastr["error"]("删除用户异常", "提示");
	                }
	                
	            }
	        });
			
		});
	}
	
	var initResetUser = function(){
		$("#user_table").delegate('.reset','click',function(){
			
			var userId = $(this).data("userId");
			
			$.ajax({
	            url:context+'/console/user/password/reset',
	            type:'POST',
	            data:{userId:userId},
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	                if(data.success){
	                	toastr["info"]("用户密码重置成功", "提示");
	                }else{
	                	toastr["error"]("用户密码重置异常", "提示");
	                }
	                
	            }
	        });
			
		});
	}
	
	var initQueryBtn = function(){
		$(".query").on('click',function(){
			var tableId = $(this).data("table");
			var table = $(tableId).DataTable();
			if(table){
				table.ajax.reload();
			}
		});
	}

	initUserTable();
	initAddUser();
	initEditUser();
	initRemoveUser();
	initResetUser();
	initQueryBtn();
	
});